package com.biomatch.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.biomatch.domain.Position;
import com.biomatch.domain.Rule;
import com.biomatch.domain.UserInfo;
import com.biomatch.dto.Pagination;
import com.biomatch.response.RestResponse;
import com.biomatch.response.RestResult;
import com.biomatch.service.RuleService;
import com.biomatch.utils.Internationalize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController("lock.rule")
@RequestMapping("/rule")
@Api(value="规则管理")
public class RuleController {

    @Autowired
    private Internationalize internationalize;

    @Autowired
    private RuleService ruleService;

    @RequestMapping(value="/saveOrUpdateRule", method = RequestMethod.POST)
    @ApiOperation(value = "添加或修改规则", response = Rule.class, httpMethod = "POST")
    public RestResult<Rule> saveOrUpdateUser(@RequestBody Rule rule) {
        Rule result = ruleService.saveOrUpdateRule(rule);
        if (result == null) {
            //没写国际化
            return RestResponse.error(internationalize.getMessage("rule.name.repeat"));
        } else {
            return RestResponse.success(result);
        }
    }

    @RequestMapping(value="/get4Page", method = RequestMethod.POST)
    @ApiOperation(value = "分页获取规则", response = Rule.class, httpMethod = "POST")
    public RestResult<Rule> get4Page(@RequestBody Pagination pagination) {
        IPage<Rule> result=ruleService.get4Page(pagination);
        return RestResponse.success(result);
    }

    @RequestMapping(value="/getAll", method = RequestMethod.POST)
    @ApiOperation(value = "获取全部规则", response = Rule.class, httpMethod = "POST")
    public RestResult<List<Rule>> getAll() {
        List<Rule> result= ruleService.getAll();
        return RestResponse.success(result);
    }

    @RequestMapping(value="/deleteRule", method = RequestMethod.POST)
    @ApiOperation(value = "删除规则", response = Rule.class, httpMethod = "POST")
        public RestResult<Rule> delete(@RequestBody Rule rule) {
        Rule result = ruleService.deleteRule(rule);
        if (result == null) {
            return RestResponse.error(internationalize.getMessage("rule.status.used"));
        } else {
            return RestResponse.success(result);
        }
    }

}
